﻿using System;
using Autodesk.Revit.DB;

namespace YArchitech.MEP
{
	public class DefinitionLineType
	{
		public DefinitionLineType()
		{
		}

		public DefinitionLineType(Document doc, Element elem, DetailCurve detailCurve, out ElementId detailLineStyleId)
		{
			ElementId elementId = null;
			if (elem.Category.Id.IntegerValue == -2008099)
			{
				string newLineStyleName = "Hy_SprinklersProtectRangeLineStyle";
				elementId = DefinitionLineType.ReturnLineStyleId(doc, detailCurve, newLineStyleName);
			}
			else if (elem.Category.Id.IntegerValue == -2001140)
			{
				string newLineStyleName2 = "Hy_MechanicalEquipmentProtectRangeLineStyle";
				elementId = DefinitionLineType.ReturnLineStyleId(doc, detailCurve, newLineStyleName2);
			}
			else if (elem.Category.Id.IntegerValue == -2008085)
			{
				string newLineStyleName3 = "Hy_FireAlarmDevicesProtectRangeLineStyle";
				elementId = DefinitionLineType.ReturnLineStyleId(doc, detailCurve, newLineStyleName3);
			}
			detailLineStyleId = elementId;
		}

		public static ElementId ReturnLineStyleId(Document doc, DetailCurve detailCurve, string newLineStyleName)
		{
			ElementId elementId = null;
			foreach (ElementId elementId2 in detailCurve.GetLineStyleIds())
			{
				Element element = doc.GetElement(elementId2);
				if (element.Name == newLineStyleName)
				{
					elementId = element.Id;
					break;
				}
			}
			if (elementId == null)
			{
				using (SubTransaction subTransaction = new SubTransaction(doc))
				{
					subTransaction.Start();
					Category category = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Lines);
					doc.Settings.Categories.NewSubcategory(category, newLineStyleName);
					subTransaction.Commit();
				}
				doc.Regenerate();
				foreach (ElementId elementId3 in detailCurve.GetLineStyleIds())
				{
					Element element2 = doc.GetElement(elementId3);
					if (element2.Name == newLineStyleName)
					{
						elementId = element2.Id;
						break;
					}
				}
			}
			return elementId;
		}
	}
}
